#!/bin/csh -f
#(ie run the cshell on this but don't read the .cshrc)

clear
echo version = 2.17 of l 2002 Nov 5
# 2002 Nov  5, 2.17: (John Collins) Make work with latexmk
# 2002 Oct 21, 2.16: replace popcross with kickxdvi
# 2000 Mar  6, 2.14: done is created as a directory (more useful than a file)
# 1999 Nov 18:  removal of a paper triggers bibtex call now
# 1999 Aug 19:  when done, `touch done` so that other
# programs can be triggered (ie latex2html!)

set myname=l1

if ( "$1" == "") then
   set paper=paper
else
   set paper=$1
endif

if ( "$paper:e" == "" ) then
   set base=$paper
   set paper=${base}.tex
else
   set base=$paper:r
endif

if !(-f $paper) then
   echo $paper does not exist\!
   exit 1;
endif

# popcross refreshes the xdvi display by briefly putting
# a window in front of the xdvi window.  This is crude but effective.
# A better way is to use the kickxdvi routine.
set kickxdvi = popcross # old method.
set kickxdvi = kickxdvi

# set overfull = Overfull
set overfull = NoOverfull
echo ALL $overfull MESSAGES WILL BE SUPPRESSED
# see Lamport page 177 for dealing with overfull boxes.
# basically, do this:
# \documentclass[12pt,draft]{article}
# draft will mark the overfull boxes and the solution
# will become obvious...

# run latex.  The cat /dev/null prevents
# latex from stopping.  Errors are reported to paper.log
cat /dev/null | latex $paper | grep -v $overfull

# determine if there was an error, by looking at paper.log:
grep "! Emergency stop." $base.log
@ EmergencyStop = ($status == 0)
# Also find if no output produced:
#     Examine only the last line of the file to pick out only the
#     message produced by TeX, and not something else with the same
#     string. 
tail -1 $base.log | grep "No pages of output."
@ NoOutput = ($status == 0)

if ($EmergencyStop || $NoOutput)then

   # begin latexerrors %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   echo create and display latex errors

   set err = `whoami`-$base
   set tmperr = /tmp/$err.tex

   echo "\documentclass[12pt]{article}" > $tmperr
   echo "\textwidth 7.5in"  >> $tmperr
   echo "\begin{document}"  >> $tmperr
   if ( $EmergencyStop ) then
        echo "Error(s) in tex file ($paper): " >> $tmperr
   else if ( $NoOutput ) then
        echo "No output from tex file ($paper): " >> $tmperr
   endif
   echo "\begin{verbatim}" >> $tmperr

   cat $base.log >> $tmperr

   echo "\end{verbatim}" >> $tmperr
   echo "\end{document}" >> $tmperr

   pushd /tmp
     latex $err.tex 
     if ( $EmergencyStop ) then
         echo xdvi display shows the last page of the errors
         set page = '+'
     else
         set page = '' 
     endif
     xdvi $err.dvi $page &
   popd
   # end   latexerrors %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   cp /tmp/$err.dvi $base.dvi
   $kickxdvi
   echo "****************************************"
   echo "* ERROR IN PAPER - fix paper.tex file\! *"
   echo "****************************************"
   # beep
   echo -n ""
   sleep 2
   # beep
   echo -n ""
   cd /tmp
   rm -f $err.aux $err.dvi $err.log $err.tex

   exit 1
else
   echo Successful run
   $kickxdvi
   if !(-f done || -d done) then
      mkdir done
   endif
   touch done

   exit

endif
